//
//  MBSpaceAboutViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceAboutViewController.h"
#import "MBSpaceContactViewController.h"
#import "MBSpaceAgreementViewController.h"

@interface MBSpaceAboutViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView             *_tableView;
}

@end

@implementation MBSpaceAboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"关于米宝兔"];
    [self setBackItem];
    
    [self onCreateUI];
}

#pragma mark - UI
- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = [self addTableheaderView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
    _tableView.tableHeaderView = [self addTableheaderView];
    [_tableView reloadData];
}

- (UIView *)addTableheaderView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 500*PERCEN)];
    bgView.backgroundColor = [UIColor clearColor];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake((bgView.width-240*PERCEN)/2., 80*PERCEN, 240*PERCEN, 240*PERCEN)];
    imgView.image = [UIImage imageNamed:@"logo_dl"];
    imgView.backgroundColor = [UIColor clearColor];
    imgView.layer.cornerRadius = imgView.height/2.;
    imgView.layer.masksToBounds = YES;
    [bgView addSubview:imgView];
    
    UILabel *nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(0,imgView.bottom + 40*PERCEN, bgView.width, 20)];
    nickNameLb.backgroundColor = [UIColor clearColor];
    nickNameLb.text = @"米宝兔";
    nickNameLb.textAlignment = NSTextAlignmentCenter;
    nickNameLb.textColor = [UIColor blackColor];
    nickNameLb.font = [UIFont systemFontOfSize:14.];
    [bgView addSubview:nickNameLb];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    UILabel *contentNameLb = [[UILabel alloc]initWithFrame:CGRectMake(0, nickNameLb.bottom, bgView.width, 20)];
    contentNameLb.backgroundColor = [UIColor clearColor];
    contentNameLb.text = [NSString stringWithFormat:@"版本号: %@",app_Version];
    contentNameLb.textAlignment = NSTextAlignmentCenter;
    contentNameLb.textColor = [UIColor blackColor];
    contentNameLb.font = [UIFont systemFontOfSize:12.];
    [bgView addSubview:contentNameLb];
    
    return bgView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //歌曲
    static NSString *cellID = @"space_about_Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *_nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(25*PERCEN, (100*PERCEN-20)/2., 200, 20)];
        _nickNameLb.backgroundColor = [UIColor clearColor];
        _nickNameLb.textAlignment = NSTextAlignmentLeft;
        _nickNameLb.textColor = UIColorFromHex(0x333333);
        _nickNameLb.font = [UIFont systemFontOfSize:13.f];
        _nickNameLb.tag = 1400;
        [cell.contentView addSubview:_nickNameLb];
        
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        UIImageView *rightView= [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-25*PERCEN-rightImg.size.width, (100*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height)];
        rightView.backgroundColor = [UIColor clearColor];
        rightView.image = rightImg;
        rightView.tag = 1402;
        [cell.contentView addSubview:rightView];
        
        UIView *_lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100*PERCEN-1, SCREEN_WIDTH, 1)];
        _lineView.backgroundColor = [UIColorFromHex(0xcccccc) colorWithAlphaComponent:.35];
        _lineView.tag = 1401;
        [cell.contentView addSubview:_lineView];
    }
    
    UILabel *nickNameLb = (UILabel *)[cell viewWithTag:1400];
    UIView *lineView = (UIView *)[cell viewWithTag:1401];
    UIImageView *rightView = (UIImageView *)[cell viewWithTag:1402];
    
    if (indexPath.row == 0) {
        nickNameLb.text = @"联系我们";
    }
    else if (indexPath.row == 1) {
        nickNameLb.text = @"应用介绍";
    }
    else if (indexPath.row == 2) {
        nickNameLb.text = @"用户协议";
        lineView.hidden = YES;
    }
    
    if (ISIPAD) {
        nickNameLb.left = 460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        lineView.frame = CGRectMake(440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 100*PERCEN-1, SCREEN_WIDTH-880*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 1);
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        rightView.frame = CGRectMake(SCREEN_WIDTH-440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.)-rightImg.size.width, (100*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height);
    } else {
        nickNameLb.left = 25*PERCEN;
        lineView.frame = CGRectMake(0, 100*PERCEN-1, SCREEN_WIDTH, 1);
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        rightView.frame = CGRectMake(SCREEN_WIDTH-25*PERCEN-rightImg.size.width, (100*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height);
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        MBSpaceContactViewController *contack = [[MBSpaceContactViewController alloc]init];
        [self.navigationController pushViewController:contack animated:YES];
    }
    else if (indexPath.row == 1) {
        MBSpaceAgreementViewController *show = [[MBSpaceAgreementViewController alloc]init];
        show.type = AgreementType_Show;
        [self.navigationController pushViewController:show animated:YES];
    }
    else if (indexPath.row == 2) {
        MBSpaceAgreementViewController *show = [[MBSpaceAgreementViewController alloc]init];
        show.type = AgreementType_Agreen;
        [self.navigationController pushViewController:show animated:YES];
    }
}


@end
